<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="部门名称">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="部门编码">
        <el-input v-model="form.code" />
      </el-form-item>

      <el-form-item label="部门负责人">
        <el-select v-model="form.manager" placeholder="请选择部门负">
          <el-option
            v-for="obj in departmentHeadList"
            :key="obj.id"
            :label="obj.username"
            :value="obj.username"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="活动形式">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deptSubmit">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeBriefnessListAPI, PostDepartmentAPI } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: [Number, String],
      require: true,
      default: ''
    }
  },
  data() {
    return {
      form: {
        code: '', // 部门编码，同级部门不可重复
        introduce: '', // 介绍
        manager: '', // 负责人名称
        name: '' // 部门名称
      },
      departmentHeadList: [] // 部门负责人列表
    }
  },
  created() {
    //   调用 获取员工简单列表 的方法
    this.getEmployeeBriefnessListFn()
  },
  methods: {
    //   获取员工简单列表
    async getEmployeeBriefnessListFn() {
      const res = await getEmployeeBriefnessListAPI()
      //   console.log(res)
      this.departmentHeadList = res.data
    },

    // 确认 - 点击事件
    async deptSubmit() {
      const res = await PostDepartmentAPI({ ...this.form, pid: this.parentId })
      // console.log(res)
      this.$emit('newDepartmentEvent', res)
    },

    // 取消 - 点击事件
    cancel() {
      this.$emit('closeDialogEvent')
    }
  }
}
</script>

<style lang="scss" scoped>
form.el-form {
  padding: 0px 60px 0px 20px;
}

.el-select {
  width: 100%;
}
</style>
